#include"Socket.hpp"
#include"Protocol.hpp"
#include <cassert>
void Usage(std::string proc)
{
    std::cout<<"\n\rUsage:"<<proc<<" serverip serverport"<<std::endl;
}

//Cliencal serverip serverport
int main(int argc,char*argv[])
{
    if(argc!=3)
    {
        Usage(argv[0]);
        exit(1);
    }
    srand(time(nullptr));
    std::string serverip=argv[1];
    uint16_t serverport=std::stoi(argv[2]);
    Sock sockfd;
    sockfd.Socket();
   bool r= sockfd.Connect(serverip,serverport);
   if(!r)return 1;
    int cnt=10;
    std::string operat="+-*/%^&!";
    std::string inbuffer_stream;
    while(cnt--)
    {
        int x=rand()%100+1;
        int y=rand()%100;
        char p=operat[rand()%operat.size()];
        Request rq(x,y,p);
        std::string package;
      rq.Serialize(&package);
      package= Encode(package);
      rq.DebugPrint();

        write(sockfd.Fd(),package.c_str(),package.size());
        char buffer[SIZE];
       ssize_t n=read(sockfd.Fd(),buffer,sizeof(buffer));
       if(n>0)
       {
        buffer[n]='\0';
        inbuffer_stream+=buffer;
        std::string content;
       bool r= Decode(inbuffer_stream,&content);
       assert(r);
        Response rp;
       r= rp.Deserialize(content);
        assert(r);
        rp.DebugPrint();
       }
       else if(n==0)break;
       else break;
    }

    return 0;
}